package main

import "fmt"

func main() {
	defer fmt.Println("main-defer-1")
	defer fmt.Println("main-defer-2")
	defer fmt.Println("main-defer-3")
	defer fmt.Println("main-defer-4")
	fmt.Println("main-1")
	fmt.Println("main-2")
	fmt.Println("main-3")
	fmt.Println("main-4")
	testpanic()

}

func testpanic() {
	//defer是按照作用域的大小来执行的，所以先执行完函数里面的defer再执行main函数里的defer
	defer fmt.Println("defer-testpanic-1")
	defer fmt.Println("defer-testpanic-2")
	defer fmt.Println("defer-testpanic-3")
	fmt.Println("testpanic-1")
	fmt.Println("testpanic-2")
	fmt.Println("testpanic-3")
	fmt.Println("testpanic-4")
	panic("程序停住了") //当panic触发程序停止执行程序开始倒序执行defer函数

	defer fmt.Println("defer-testpanic-4")
}
